﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimulationPTSystem.DAL
{
    /// <summary>
    /// 模拟实战训练系统DAL
    /// </summary>
    public class SimulationPTDAL
    {
        static SimulationPTContext _context = SimulationPTContext.Instance;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public static bool Initialize()
        {
            return _context.Database.CreateIfNotExists();
        }

        public static bool Login(string userName, string password)
        {
            if (userName != "admin" || password != "admin")
                return false;

            return true;
        }

        /// <summary>
        /// 获取所有团队信息
        /// </summary>
        /// <returns></returns>
        public static async Task<List<TeamInfo>> GetTeamInfosAsync()
        {
            return await _context.TeamInfo.ToListAsync();
        }

        /// <summary>
        /// 获取团队信息列表
        /// </summary>
        /// <returns></returns>
        public static async Task<List<TeamInfo>> GetTeamInfosAsync(string code, string name)
        {
            var query = _context.TeamInfo.AsQueryable();
            if (!string.IsNullOrEmpty(code))
                query = query.Where(c => c.Code.Contains(code));
            if (!string.IsNullOrEmpty(name))
                query = query.Where(c => c.Name.Contains(name));

            return await query.ToListAsync();
        }

        /// <summary>
        /// 删除团队信息
        /// </summary>
        /// <param name="teamInfo"></param>
        /// <returns></returns>
        public static bool DeleteTeamInfo(TeamInfo teamInfo)
        {
            if (teamInfo == null)
                return false;

            _context.TeamInfo.Remove(teamInfo);
            _context.SaveChanges();
            return true;
        }
        /// <summary>
        /// 添加团队信息
        /// </summary>
        /// <param name="teamInfo"></param>
        public static void AddTeamInfo(TeamInfo teamInfo)
        {
            _context.TeamInfo.Add(teamInfo);
            _context.SaveChanges();
        }
        /// <summary>
        /// 更新团队信息
        /// </summary>
        /// <param name="teamInfo"></param>
        public static void UpdateTeamInfo(TeamInfo teamInfo)
        {
            _context.SaveChanges();
        }

        /// <summary>
        /// 获取成员信息列表
        /// </summary>
        /// <param name="code"></param>
        /// <param name="name"></param>
        /// <param name="ageFrom"></param>
        /// <param name="ageTo"></param>
        /// <param name="gender"></param>
        /// <returns></returns>
        public static List<ChengYuanInfo> GetChengYuanInfos(string code,string name,int ageFrom ,int ageTo,string gender)
        {
            var query = _context.ChengYuanInfo.AsQueryable();
            if (!string.IsNullOrEmpty(code))
                query= query.Where(c => c.Code.Contains(code));
            if (!string.IsNullOrEmpty(name))
                query = query.Where(c => c.Name.Contains(name));
            if (ageFrom != -1)
                query = query.Where(c => c.Age >= ageFrom);
            if (ageTo != -1)
                query = query.Where(c => c.Age <= ageTo);
            if (!string.IsNullOrEmpty(gender))
                query = query.Where(c => c.Gender == gender);

            return query.ToList();
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="count"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static List<ChengYuanInfo> GetChengYuanInfos(int pageIndex,int count,string name)
        {
            if (pageIndex <= 0)
                return new List<ChengYuanInfo>();

            var query = _context.ChengYuanInfo.AsQueryable();
            if (!string.IsNullOrEmpty(name))
                query = query.Where(c => c.Name.Contains(name));
            query = query.OrderByDescending(c=>c.Id).Skip((pageIndex - 1) * count).Take(count);

            return query.ToList();
        }

        /// <summary>
        /// 删除成员信息
        /// </summary>
        /// <param name="chengYuanInfo"></param>
        /// <returns></returns>
        public static bool DeleteChengYuanInfo(ChengYuanInfo chengYuanInfo)
        {
            if (chengYuanInfo == null)
                return false;

            _context.ChengYuanInfo.Remove(chengYuanInfo);
            _context.SaveChanges();
            return true;
        }
        /// <summary>
        /// 添加成员信息
        /// </summary>
        /// <param name="chengYuanInfo"></param>
        public static void AddChengYuanInfo(ChengYuanInfo chengYuanInfo)
        {
            _context.ChengYuanInfo.Add(chengYuanInfo);
            _context.SaveChanges();
        }
        /// <summary>
        /// 更新成员信息
        /// </summary>
        /// <param name="chengYuanInfo"></param>
        public static void UpdateChengYuanInfo(ChengYuanInfo chengYuanInfo)
        {
            _context.SaveChanges();
        }
        /// <summary>
        /// 添加当前训练团队信息
        /// </summary>
        /// <param name="teamInfo"></param>
        public static void AddCurrentTrainingTeamInfo(TeamInfo teamInfo)
        {
            if (_context.CurrentTrainingTeamInfo.Count() > 0)
                _context.CurrentTrainingTeamInfo.RemoveRange(_context.CurrentTrainingTeamInfo.ToList());
            _context.CurrentTrainingTeamInfo.Add(new CurrentTrainingTeamInfo
            {
                TeamInfoId = teamInfo.Id,
                StartTime = DateTime.Now,
                TeamTrainingRecUUID = Guid.NewGuid().ToString(),
            });
            _context.SaveChanges();
        }
        /// <summary>
        /// 获取当前训练团队信息
        /// </summary>
        /// <returns></returns>
        public static CurrentTrainingTeamInfo GetCurrentTrainingTeamInfo()
        {
            //if (_context.CurrentTrainingTeamInfo.Count() == 0)
            //{
            //    AddCurrentTrainingTeamInfo(_context.TeamInfo.ToList().FirstOrDefault());
            //};
            return _context.CurrentTrainingTeamInfo.ToList().LastOrDefault();
        }

        /// <summary>
        /// 添加团队训练记录
        /// </summary>
        /// <param name="teamTrainingRec"></param>
        public static void AddTeamTrainingRec(TeamTrainingRec teamTrainingRec)
        {
            _context.TeamTrainingRec.Add(teamTrainingRec);
            _context.SaveChanges();
        }

        /// <summary>
        /// 获取团队训练记录
        /// </summary>
        /// <param name="teamCode"></param>
        /// <param name="teamName"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public static async Task<List<TeamTrainingRec>> GetTeamTrainingRecs(string teamCode, string teamName, DateTime startTime, DateTime endTime)
        {
            var teamTrainingRecs = _context.TeamTrainingRec.AsQueryable();
            if (!string.IsNullOrEmpty(teamCode))
                teamTrainingRecs = teamTrainingRecs.Where(t => t.TeamInfo.Code == teamCode);
            if (!string.IsNullOrEmpty(teamName))
                teamTrainingRecs = teamTrainingRecs.Where(t => t.TeamInfo.Name == teamName);
            if (startTime != null && startTime != new DateTime())
                teamTrainingRecs = teamTrainingRecs.Where(t => t.StartTime >= startTime);
            if (endTime != null && endTime != new DateTime())
            {
                var trueEndTime = endTime.AddDays(1);
                teamTrainingRecs = teamTrainingRecs.Where(t => t.StartTime < trueEndTime);
            }

            return await teamTrainingRecs.ToListAsync();
        }

        /// <summary>
        /// 获取成员训练记录
        /// </summary>
        /// <param name="chengYuanCode"></param>
        /// <param name="chengYuanName"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public static async Task<List<ChengYuanTrainingRec>> GetChengYuanTrainingRecs(string chengYuanCode,string chengYuanName, DateTime startTime, DateTime endTime)
        {
            var chengYuanTrainingRecs = _context.ChengYuanTrainingRec.AsQueryable();
            if (!string.IsNullOrEmpty(chengYuanCode))
                chengYuanTrainingRecs = chengYuanTrainingRecs.Where(t => t.ChengYuan.Code == chengYuanCode);
            if (!string.IsNullOrEmpty(chengYuanName))
                chengYuanTrainingRecs = chengYuanTrainingRecs.Where(t => t.ChengYuan.Name == chengYuanName);
            if (startTime != null && startTime != new DateTime())
                chengYuanTrainingRecs = chengYuanTrainingRecs.Where(t => t.TrainingDate >= startTime);
            if (endTime != null && endTime != new DateTime())
            {
                var trueEndTime = endTime.AddDays(1);
                chengYuanTrainingRecs = chengYuanTrainingRecs.Where(t => t.TrainingDate < trueEndTime);
            }

            return await chengYuanTrainingRecs.ToListAsync();
        }
    }
}
